package com.unitech.order.converter;

import com.unitech.order.api.beans.OrderInfo;
import com.unitech.order.api.enums.OrderStatusType;
import com.unitech.order.dao.entity.Order;

import java.time.format.DateTimeFormatter;

/**
 * 用于数据库实体类order的转换
 */
public class OrderConverter {

    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    // 转换为OrderInfo response对象
    public static OrderInfo convertToOrderInfo(Order entity) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);

        return OrderInfo.builder()
                .id(entity.getId())
                .customer(entity.getCustomer())
                .createTime(entity.getCreateTime().format(formatter))
                .total(entity.getTotal())
                .state(OrderStatusType.convert(entity.getState()).getDescription())
                .version(entity.getVersion())
                .build();
    }
}
